package com.sdy.healthcode.biz.dto;

import com.sdy.common.utils.StringUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Data
@ApiModel(value = "家属信息")
@Accessors(chain = true)
public class FamilyMember {
    @ApiModelProperty(value = "姓名", required = true)
    private String name;
    @ApiModelProperty(value = "手机号", required = true)
    private String phone;

    public static final String SPLITTER_1 = "##";
    public static final String SPLITTER_2 = "@@";

    public static List<FamilyMember> fromStr(String s) {
        if (s == null) {
            return Collections.emptyList();
        }
        List<FamilyMember> r = new ArrayList<>();
        for (String s1 : s.split(SPLITTER_1)) {
            if (StringUtil.isBlank(s1)) {
                continue;
            }
            FamilyMember familyMember = new FamilyMember();
            String[] s2 = s1.split(SPLITTER_2);
            familyMember.setName(s2[0]);
            familyMember.setPhone(s2[1]);
            r.add(familyMember);
        }
        return r;
    }
    
    public static FamilyMember fromStrToSingleObject(String s) {
        List<FamilyMember> l = fromStr(s);
        return l.isEmpty() ? null : l.get(0);
    }

    @Override
    public String toString() {
        return name + SPLITTER_2 + phone;
    }

    public static String toStr(List<FamilyMember> addressList) {
        return addressList.stream().map(FamilyMember::toString).collect(Collectors.joining(SPLITTER_1));
    }
}
